package com.infore.water.service;

import com.infore.water.dao.DataDao;
import com.infore.water.dao.RealDataDao;
import com.infore.water.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DataService{

    @Autowired
    private DataDao dataDao;

    @Autowired
    private RealDataDao realDataDao;

    public List<AnalyzeData> getStationAnalyse(String stationString, String startTime, String endTime, String dataType) throws ParseException {
        List<AnalyzeData> list = new ArrayList<>();
        List<Map<String, Object>> dataList;
        SimpleDateFormat sdf;
        SimpleDateFormat asdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(dataType.equals("0")){
            sdf = new SimpleDateFormat("yyyyMMddHH");
            dataList = dataDao.getMuchHourStation(stationString,sdf.format(asdf.parse(startTime)),sdf.format(asdf.parse(endTime)));

        }else {
//            if(dataType.equals("1")){
            sdf = new SimpleDateFormat("yyyyMMdd");
            dataList =  dataDao.getMuchDayStation(stationString,sdf.format(asdf.parse(startTime)),sdf.format(asdf.parse(endTime)));
        }
        for(int i=0;i<dataList.size();i++){
            boolean flag = true;
            for(int j=0;j<list.size();j++){
                if(dataList.get(i).get("projectId").toString().equals(list.get(j).getProjectId().toString())){
                    flag = false;
                    List<Station> stationList = list.get(j).getStationList();
                    boolean sflag = true;
                    for(int z=0;z<stationList.size();z++){
                        if(dataList.get(i).get("stationId").toString().equals(stationList.get(z).getStationId().toString())){
                            sflag = false;
                            Value value = new Value();
                            value.setValue(Double.parseDouble(dataList.get(i).get("value").toString()));
                            value.setTime(asdf.format(sdf.parse(dataList.get(i).get("time").toString())));
                            stationList.get(z).getValueList().add(value);
                        }
                    }
                    if (sflag) {
                        Station station = new Station();
                        station.setStationId(Integer.parseInt(dataList.get(i).get("stationId").toString()));
                        station.setStationName(dataList.get(i).get("stationName").toString());
                        List<Value> valueList = new ArrayList<>();
                        Value value = new Value();
                        value.setValue(Double.parseDouble(dataList.get(i).get("value").toString()));
                        value.setTime(asdf.format(sdf.parse(dataList.get(i).get("time").toString())));
                        valueList.add(value);
                        station.setValueList(valueList);
                        list.get(j).getStationList().add(station);
                    }
                }
            }
            if(flag){
                AnalyzeData analyzeData = new AnalyzeData();
                analyzeData.setProjectId(Integer.parseInt(dataList.get(i).get("projectId").toString()));
                analyzeData.setProjectName(dataList.get(i).get("projectName").toString());
                analyzeData.setProjectCname(dataList.get(i).get("cname").toString());
                analyzeData.setUnit(dataList.get(i).get("unit").toString());
                List<Station> stationList = new ArrayList<>();
                Station station = new Station();
                station.setStationId(Integer.parseInt(dataList.get(i).get("stationId").toString()));
                station.setStationName(dataList.get(i).get("stationName").toString());
                List<Value> valueList = new ArrayList<>();
                Value value = new Value();
                value.setValue(Double.parseDouble(dataList.get(i).get("value").toString()));
                value.setTime(asdf.format(sdf.parse(dataList.get(i).get("time").toString())));
                valueList.add(value);
                station.setValueList(valueList);
                stationList.add(station);
                analyzeData.setStationList(stationList);
                list.add(analyzeData);
            }
        }
        return list;
    }

    public List<Map<String,Object>>  getContrastData(String type, String time, String dataType,String stationId) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dataSdf;
        List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
        List<Map<String,Object>> chainList = new ArrayList<Map<String, Object>>();
        Calendar c = Calendar.getInstance();
        c.setTime(sdf.parse(time));
        Date chainDate = null;
        if("0".equals(type)){//同比
            c.add(Calendar.YEAR, -1);
            chainDate = c.getTime();
        }else if("1".equals(type)){//环比
            if("0".equals(dataType)){
                c.add(Calendar.DATE, -1);
                chainDate = c.getTime();
            }else if("1".equals(dataType)){
                c.add(Calendar.MONTH, -1);
                chainDate = c.getTime();
            }
        }
        if("0".equals(dataType)){//日
            dataSdf = new SimpleDateFormat("yyyyMMdd");
            list = realDataDao.queryHistoryHourData(stationId,dataSdf.format(sdf.parse(time))+"00",dataSdf.format(sdf.parse(time))+"23");
            chainList = realDataDao.queryHistoryHourData(stationId,dataSdf.format(chainDate)+"00",dataSdf.format(chainDate)+"23");
            dataSdf = new SimpleDateFormat("yyyyMMddHH");
        }else {//月
            dataSdf = new SimpleDateFormat("yyyyMM");
            list = realDataDao.queryHistoryDayData(stationId,dataSdf.format(sdf.parse(time))+"00",dataSdf.format(sdf.parse(time))+"31");
            chainList = realDataDao.queryHistoryDayData(stationId,dataSdf.format(chainDate)+"00",dataSdf.format(chainDate)+"31");
            dataSdf = new SimpleDateFormat("yyyyMMdd");
        }
        List<Map<String,Object>> listAll = new ArrayList<Map<String, Object>>();
        if(list!=null&list.size()!=0) {
               for(int i=0;i<list.size();i++){
                   boolean flag = true;
                   for(int j=0;j<listAll.size();j++){
                        if(list.get(i).get("projectId").toString().equals(listAll.get(j).get("projectId").toString())){
                            flag = false;
                            Map<String,Object> map = new HashMap<>();
                            map.put("time",sdf.format(dataSdf.parse(list.get(i).get("time").toString())));
                            map.put("value",list.get(i).get("value"));
                            ((List<Map<String,Object>>)listAll.get(j).get("list")).add(map);
                        }
                   }
                   if(flag){
                        Map<String,Object> map = new HashMap<>();
                        map.put("projectId",list.get(i).get("projectId"));
                        map.put("projectName",list.get(i).get("projectName"));
                        map.put("stationId",list.get(i).get("stationId"));
                        map.put("stationName",list.get(i).get("stationName"));
                        Map<String,Object> vmap = new HashMap<>();
                        vmap.put("time",sdf.format(dataSdf.parse(list.get(i).get("time").toString())));
                        vmap.put("value",list.get(i).get("value"));
                        List<Map<String,Object>> listmap = new ArrayList<>();
                        listmap.add(vmap);
                        map.put("list",listmap);
                        map.put("compareList", new ArrayList<Map<String,Object>>());
                        listAll.add(map);
                   }
               }
        }

        if(chainList!=null&chainList.size()!=0) {
            for(int i=0;i<chainList.size();i++){
                boolean flag = true;
                for(int j=0;j<listAll.size();j++){
                    if(chainList.get(i).get("projectId").toString().equals(listAll.get(j).get("projectId").toString())){
                        flag = false;
                        Map<String,Object> map = new HashMap<>();
                        map.put("time",sdf.format(dataSdf.parse(chainList.get(i).get("time").toString())));
                        map.put("value",chainList.get(i).get("value"));
                        ((List<Map<String,Object>>)listAll.get(j).get("compareList")).add(map);
                    }
                }
                if(flag){
                    Map<String,Object> map = new HashMap<>();
                    map.put("projectId",chainList.get(i).get("projectId"));
                    map.put("projectName",chainList.get(i).get("projectName"));
                    map.put("stationId",chainList.get(i).get("stationId"));
                    map.put("stationName",chainList.get(i).get("stationName"));
                    Map<String,Object> vmap = new HashMap<>();
                    vmap.put("time",sdf.format(dataSdf.parse(chainList.get(i).get("time").toString())));
                    vmap.put("value",chainList.get(i).get("value"));
                    List<Map<String,Object>> listmap = new ArrayList<>();
                    listmap.add(vmap);
                    map.put("compareList",listmap);
                    listAll.add(map);
                }
            }
        }
        return listAll;
    }

    public List<Map<String, Object>> getAbnormalData(String stationId, String startTime, String endTime,String projects) throws Exception {
        SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat min =  new SimpleDateFormat("yyyyMMddHHmm");
        List<Map<String,Object>> list;
        if("0".equals(projects)){
            list = dataDao.getAbnormalData(stationId,min.format(sdf.parse(startTime+" 00:00:00")),min.format(sdf.parse(endTime+" 23:59:59")));
        }else {
            list = dataDao.getAbnormalDataByProject(stationId,min.format(sdf.parse(startTime+" 00:00:00")),min.format(sdf.parse(endTime+" 23:59:59")),projects);
        }
        for(int i=0;i<list.size();i++){
            list.get(i).put("time",sdf.format(min.parse(list.get(i).get("time").toString())));
        }
        return list;
    }

    public List<Map<String, Object>> getProjectArea(String userId) {
        return dataDao.getProjectArea();
    }

    public int setProjectArea(String projectId, String upper, String flower,String level_1,String level_2,String level_3) {
        if((Double.parseDouble(flower)<Double.parseDouble(level_1))&&(Double.parseDouble(level_1)<Double.parseDouble(level_2))&&(Double.parseDouble(level_2)<Double.parseDouble(level_3))&&(Double.parseDouble(level_3)<Double.parseDouble(upper))){

        }else if((Double.parseDouble(flower)>Double.parseDouble(level_1))&&(Double.parseDouble(level_1)>Double.parseDouble(level_2))&&(Double.parseDouble(level_2)>Double.parseDouble(level_3))&&(Double.parseDouble(level_3)>Double.parseDouble(upper))){

        }else {
            return -1;
        }
        return dataDao.setProjectArea(projectId,upper,flower,level_1,level_2,level_3);
    }

    public List<Map<String, Object>> getRecentlyData(String stationId) throws ParseException {
        Date date = new Date();
        SimpleDateFormat nsdf = new SimpleDateFormat("yyyy-MM-dd HH");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.set(Calendar.DATE, c.get(Calendar.DATE) - 1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
        List<Map<String,Object>> list = dataDao.getRecentlyData(stationId,sdf.format(c.getTime()),sdf.format(date));
//        List<Map<String,Object>> list = dataDao.getRecentlyData(stationId,"2017112017","2017112117");
        List<Map<String,Object>> datalist = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            boolean flag = true;
            for(int j=0;j<datalist.size();j++){
                if(list.get(i).get("projectId").equals(datalist.get(j).get("projectId"))){
                    flag = false;
                    Map<String,Object> listMap = new HashMap<>();
                    listMap.put("time",nsdf.format(sdf.parse(list.get(i).get("time").toString())));
                    listMap.put("avg",list.get(i).get("avg"));
                    ((List<Map<String,Object>>)datalist.get(j).get("dataList")).add(listMap);
                }
            }
            if(flag){
                Map<String,Object> map = new HashMap<>();
                map.put("projectId",list.get(i).get("projectId"));
                map.put("projectName",list.get(i).get("projectName"));
                map.put("unit",list.get(i).get("unit"));
                map.put("stationId",list.get(i).get("stationId"));
                map.put("stationName",list.get(i).get("stationName"));
                List<Map<String,Object>> mapList = new ArrayList<>();
                Map<String,Object> listMap = new HashMap<>();
                listMap.put("time",nsdf.format(sdf.parse(list.get(i).get("time").toString())));
                listMap.put("avg",list.get(i).get("avg"));
                mapList.add(listMap);
                map.put("dataList",mapList);
                datalist.add(map);
            }
        }
        return datalist;
    }
}
